package com.swinw.asumall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TowTask {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程启动了");
            int a = 10 / 2;
            System.out.println("任务1线程结束");
            return a;
        }, executorService);

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二线程启动");
            System.out.println("任务二线程结束");
            return "任务2";
        }, executorService);
        CompletableFuture<String> future3 = future1.thenCombineAsync(future2, (res1, res2) -> {
            System.out.println("任务3开始执行");
            System.out.println("任务1执行结果 " + res1);
            System.out.println("任务2执行结果 " + res2);
            System.out.println("任务3执行结束");
            return res1 + "++" + res2;
        }, executorService);
        System.out.println(future3.get());
        //能够感知前两个任务的执行结果，但是眉宇返回值
//        future1.thenAcceptBothAsync(future2,(res1,res2)->{
//            System.out.println("任务3开始执行");
//            System.out.println("任务1执行结果 " + res1);
//            System.out.println("任务2执行结果 " + res2);
//            System.out.println("任务3执行结束");
//        },executorService);
//        future1.runAfterBothAsync(future2,()->{
//            System.out.println("任务3开始执行");
//        },executorService);

    }
}
